export {}
//? 接口定义对象类型
interface User {
  name: string;
}

const user: User = { name: "xuzy" };

//必选属性和必选方法
interface User1 {
  name: string; //必选属性
  hi: (num: number) => number; //必选方法
  hi2: () => void;
  hi3(): void;
}

const user1: User1 = {
  name: "xuzy",
  hi: (num: number) => num,
  hi2: () => {},
  hi3: () => {},
};

// 可选属性和可选方法
interface User2 {
  name?: string;
  hi?: () => void;
}

const user2: User2 = {
  name: "xuzy",
};

// 只读属性和只读方法
interface User3 {
  readonly name: string;
  readonly getName: (_name: string) => string;
}

const user3: User3 = {
  name: "xuzy",
  getName: (a: string) => "b",
};

//! user3.name = '222' 不可重新赋值

// 任意属性和任意方法
interface User4 {
  name: string;
  [prop: string]: any; //索引签名 接收任意的属性名，只要是字符串类型的就行
}

// 这样就可以进行扩展了 灵活性提升 请注意 索引签名的参数类型必须是'string','number','symbol'或模板文本类型
let user4: User4 = {
  name: "xuzy",
  age: 24,
  mm: "mm",
};

// 接口定义函数类型
interface Say {
  (str: string): string;
}

const say: Say = (str) => str;

// 接口继承
interface User5 {
  name: string;
}

interface User6 extends User5 {
  age: number;
}

const user6: User6 = {
  name: "xuzy",
  age: 25,
};

// 接口能被实现
class People implements User6 {
  name = "xuzy";
  age = 25;
}
